const toString = Object.prototype.toString
function isObject (value) {
  return toString.call(value) === '[object Object]'
}
function isString (value) {
  return toString.call(value) === '[object String]'
}
function isDefault (value) {
  return value === void 0
}
//统一处理传参
function arrangeParams (args){
  let name, query = {}, 
	  queryStr = null, 
	  path,isName = false

  switch (true) {
    case isObject(args):
      ({name, query} = args)
      break
    case isString(args):
      name = args
      break
    default:
      throw new Error('参数必须是对象或者字符串')
  }
  if (isObject(query)) {
    queryStr = encodeURIComponent(JSON.stringify(query))
  } else {
    throw new Error('query数据必须是Object')
  }

  this.$minRouter.forEach(item => {
    if (item.name === name) {
      path = item.path
      isName = true
    }
  })
  if (!isName) {
    throw new Error(`没有${name}页面`)
  }
  return {queryStr,path}
}

function navigateTo (args) {
  let queryStr = arrangeParams.call(this,args).queryStr
  let path = arrangeParams.call(this,args).path
  
  uni.navigateTo({
	url: `/${path}?query=${queryStr}`,
	animationType:args.animationType,
	animationDuration:args.animationDuration,
	events:args.events,
	success:args.success,
	fail:args.fail,
	complete:args.complete
  })
}

function redirectTo (args) {
  let queryStr = arrangeParams.call(this,args).queryStr
  let path = arrangeParams.call(this,args).path
  
  uni.redirectTo({
	url: `/${path}?query=${queryStr}`,
	success:args.success,
	fail:args.fail,
	complete:args.complete
  })
}

function reLaunch (args) {
  let queryStr = arrangeParams.call(this,args).queryStr
  let path = arrangeParams.call(this,args).path
  
  uni.reLaunch({
	url: `/${path}?query=${queryStr}`,
	success:args.success,
	fail:args.fail,
	complete:args.complete
  })
}

function switchTab (args) {
  let path = arrangeParams.call(this,args).path
  
  uni.switchTab({
	url: `/${path}`,
	success:args.success,
	fail:args.fail,
	complete:args.complete
  })
}

function navigateBack (args) {

  uni.navigateBack({
	delta: args?args.delta : '',
	animationType: args?args.animationType: '',
	animationDuration:args?args.animationDuration: ''
  })
}

function preloadPage (args) {
  let queryStr = arrangeParams.call(this,args).queryStr
  let path = arrangeParams.call(this,args).path
  
  uni.preloadPage({
	url: `/${path}?query=${queryStr}`,
	fail:args.fail,
	complete:args.complete
  })
}

function getParams () {
  const query = this.$root.$mp.query.query
  if (query) {
    return JSON.parse(decodeURIComponent(query))
  } else {
    return {}
}}
function install (Vue) {
  Vue.mixin({
    beforeCreate: function () {
      if (!isDefault(this.$options.minRouter)) {
        Vue._minRouter = this.$options.minRouter
      }
    }
  })
  Object.defineProperty(Vue.prototype, '$minRouter', {
    get: function () {
      return Vue._minRouter._router
    }
  })
  Object.defineProperty(Vue.prototype, '$getParams', {
    get: function () {
      return Vue._minRouter.getParams
    }
  })
  Object.defineProperty(Vue.prototype, '$navigateTo', {
    get: function () {
      return Vue._minRouter.navigateTo
    }
  })
  Object.defineProperty(Vue.prototype, '$redirectTo', {
    get: function () {
      return Vue._minRouter.redirectTo
    }
  })
  Object.defineProperty(Vue.prototype, '$reLaunch', {
    get: function () {
      return Vue._minRouter.reLaunch
    }
  })
  Object.defineProperty(Vue.prototype, '$switchTab', {
    get: function () {
      return Vue._minRouter.switchTab
    }
  })
  Object.defineProperty(Vue.prototype, '$navigateBack', {
    get: function () {
      return Vue._minRouter.navigateBack
    }
  })
  Object.defineProperty(Vue.prototype, '$preloadPage', {
    get: function () {
      return Vue._minRouter.preloadPage
    }
  })
 }
function MinRouter (options) {
  if (!(this instanceof MinRouter)) {
    throw Error("MinRouter是一个构造函数，应该用`new`关键字调用")
  }
  isDefault(options) && (options = {})
  this.options = options
  this._router = options.routes || []
}
MinRouter.install = install
MinRouter.prototype.getParams = getParams
MinRouter.prototype.navigateTo = navigateTo
MinRouter.prototype.redirectTo = redirectTo
MinRouter.prototype.reLaunch = reLaunch
MinRouter.prototype.switchTab = switchTab
MinRouter.prototype.navigateBack = navigateBack
MinRouter.prototype.preloadPage = preloadPage
export default MinRouter